package com.ruoyi.seckill;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.SeckillGoods;
import com.ruoyi.system.service.ISeckillGoodsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/seckill")
public class SeckillController extends BaseController {

    @Resource
    private SeckillService seckillService;
    @Resource
    private ISeckillGoodsService seckillGoodsService;

    @PostMapping("/seckill")
    public AjaxResult seckill(@RequestBody SeckillMessage message) {
        try {
            return seckillService.seckill(message.getGoodsId(), message.getUserId());
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/seckillList")
    public TableDataInfo seckillList(SeckillGoods seckillGoods) {
        startPage();
        List<SeckillGoods> list = seckillGoodsService.selectSeckillGoodsList(seckillGoods);
        return getDataTable(list);
    }

    @GetMapping("/refrestSeckillStock")
    public AjaxResult refrestSeckillStock(SeckillGoods seckillGoods) {
        return seckillGoodsService.refrestSeckillStock(seckillGoods);
    }
}
